<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                新增商户
            </el-header>
            <el-main>
                <el-form label-width="110px" ref="merchants" :rules="rules" :model="merchants" size="mini"
                         :label-position="formAlignLeft">
                    <el-form-item label="商户名称 : " prop="name">
                        <el-input v-model="merchants.name" placeholder="请输入商户名称"></el-input>
                    </el-form-item>
                    <el-form-item label="商户号 : " prop="code">
                        <el-input v-model="merchants.code" placeholder="请输入商户号"></el-input>
                    </el-form-item>
                    <el-form-item label="机构UUID : " prop="instUuid" v-show="false">
                        <el-input v-model="merchants.instUuid"></el-input>
                    </el-form-item>
                    <el-form-item label="机构名称 : " prop="instName">
                        <el-select v-model="merchants.instName" filterable placeholder="请选择机构" style="width: 100%;" @change='getValue'>
                            <el-option
                                    v-for="inst in instOptions"
                                    :key="inst.creCode"
                                    :label="inst.name"
                                    :value="inst.name">
                                {{inst.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="信用代码 : " prop="creCode">
                        <el-input v-model="merchants.creCode" placeholder="选择机构后自动填充" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="联系人 : " prop="linkMan">
                        <el-input v-model="merchants.linkMan" placeholder="请输入联系人"></el-input>
                    </el-form-item>
                    <el-form-item label="联系电话 : " prop="linkTel">
                        <el-input v-model="merchants.linkTel" placeholder="请输入联系电话"></el-input>
                    </el-form-item>
                    <el-form-item label="用户UUID : " prop="userUuid" v-show="false">
                        <el-input v-model="merchants.userUuid"></el-input>
                    </el-form-item>
                    <el-form-item label="用户绑定 : " prop="userName">
                        <el-select v-model="merchants.userName" filterable placeholder="请选择用户" style="width: 100%;" @change='getUserValue'>
                            <el-option
                                    v-for="inst in userOptions"
                                    :key="inst.uuid"
                                    :label="inst.name"
                                    :value="inst.name">
                                {{inst.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        保存
                    </el-button>
                    <el-button @click="editOut">
                        取消
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../utils/webUtils.js';
    import {getDataForSP, writePageInfo} from "../../utils/webUtils";

    export default {
        name: "addMerchants",
        data() {
            return {
                formAlignLeft: 'left',
                uuid: '',
                merchants: {
                    name: '',
                    code: '',
                    instUuid: '',
                    instName: '',
                    creCode: '',
                    linkMan: '',
                    linkTel: '',
                    userUuid:'',
                    userName:''
                },
                instOptions: [],
                userOptions: [],
                rules: {
                    name: [
                        {required: true, message: '商户名称不能为空', trigger: 'blur'},
                    ],
                    code: [
                        {required: true, message: '商户号不能为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        mounted() {
            this.queryInstitutions();
            this.queryUsers();
        },
        methods: {
            //查询机构
            queryInstitutions() {
                this.axios
                    .request({
                        method: "get",
                        url: "./kwp/kdi/inst/search/findAll",
                        params: {}
                    })
                    .then(response => {
                        this.instOptions = response.data.content;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            //查询用户
            queryUsers() {
                this.axios
                    .request({
                        method: "get",
                        url: "./kwp/kdi/user/search/findByFilter",
                        params: {}
                    })
                    .then(response => {
                        this.userOptions = response.data.content;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            //下拉框值改变时
            getValue(){
                this.instOptions.forEach( list =>{
                    if(list.name === this.merchants.instName){
                        this.merchants.creCode = list.creCode
                        this.merchants.instUuid = list.uuid
                    }
                })
            },
            getUserValue(){
                this.userOptions.forEach( list =>{
                    if(list.name === this.merchants.userName){
                        this.merchants.userName = list.name
                        this.merchants.userUuid = list.uuid
                    }
                })
            },
            submit() {
                this.$refs['merchants'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.merchants = replaceSpacesObj(this.merchants);
                    this.axios.request({
                        method: "post",
                        url: "./ope/kdi/merch",
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: {
                            name: this.merchants.name,
                            code: this.merchants.code,
                            instUuid: this.merchants.instUuid,
                            instName: this.merchants.instName,
                            creCode: this.merchants.creCode,
                            linkMan: this.merchants.linkMan,
                            linkTel: this.merchants.linkTel,
                            userUuid: this.merchants.userUuid,
                            userName: this.merchants.userName
                        }
                    }).then(() => {
                        this.endProcessing();
                    }).catch(error => {
                        console.log(error);
                        this.info = '商户新增失败';
                        this.infoShow = true;
                    });
                });
            },
            //获取所有数据
            queryMerchants() {
                this.axios.request({
                    method: "get",
                    url: "./ope/kdi/merch/search/findAll",
                    params: getDataForSP(this.userPage)
                }).then(response => {
                    this.userData = response.data.content;
                    writePageInfo(this.userPage, response.data);
                }).catch(error => {
                    console.log(error);
                });
            },
            endProcessing() {
                this.info = this.info + '新增成功,两秒后关闭';
                this.infoShow = true;
                setTimeout(() => {
                    this.infoShow = false;
                    this.$emit("editState", false);
                    this.cleanMerchants();
                    this.info = '';
                }, 2000);
            },
            cleanMerchants() {
                this.merchants = {
                    name: '',
                    code: '',
                    instUuid: '',
                    instName: '',
                    creCode: '',
                    linkMan: '',
                    linkTel: '',
                    userUuid: '',
                    userName: ''
                };
            },
            editOut() {
                this.infoShow = false;
                this.cleanMerchants();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 334px;
        height: 100%;
        z-index: 100;
    }


</style>